<?php
session_start();

// 定义初始属性和事件列表
$initialAttributes = [
    "智力" => 0,
    "体质" => 0,
    "魅力" => 0,
    "财富" => 0
];

$events = [
    [
        "description" => "你参加了一场知识竞赛，凭借高智力获得了一等奖，智力 +2",
        "condition" => function ($attributes) {
            return $attributes["智力"] >= 3;
        },
        "effect" => function (&$attributes) {
            $attributes["智力"] += 2;
        }
    ],
    [
        "description" => "你坚持每天锻炼，体质得到了提升，体质 +1",
        "condition" => function ($attributes) {
            return true;
        },
        "effect" => function (&$attributes) {
            $attributes["体质"] += 1;
        }
    ],
    [
        "description" => "你在社交活动中展现出魅力，结识了很多朋友，魅力 +1",
        "condition" => function ($attributes) {
            return $attributes["魅力"] >= 2;
        },
        "effect" => function (&$attributes) {
            $attributes["魅力"] += 1;
        }
    ],
    [
        "description" => "你投资失败，财富减少了一些，财富 -1",
        "condition" => function ($attributes) {
            return $attributes["财富"] >= 1;
        },
        "effect" => function (&$attributes) {
            $attributes["财富"] -= 1;
        }
    ]
];

// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST["start"])) {
        $totalPoints = 10;
        $intelligence = (int)$_POST["intelligence"];
        $constitution = (int)$_POST["constitution"];
        $charm = (int)$_POST["charm"];
        $wealth = (int)$_POST["wealth"];

        if ($intelligence + $constitution + $charm + $wealth == $totalPoints) {
            $_SESSION["attributes"] = [
                "智力" => $intelligence,
                "体质" => $constitution,
                "魅力" => $charm,
                "财富" => $wealth
            ];
            $_SESSION["log"] = [];
            header("Location: simulate.php");
            exit;
        } else {
            $error = "属性点总和必须为 $totalPoints";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人生重启模拟器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }

        form {
            margin-top: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            margin-bottom: 10px;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>
    <h1>人生重启模拟器</h1>
    <?php if (isset($error)): ?>
        <p class="error"><?php echo $error; ?></p>
    <?php endif; ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <label for="intelligence">智力 (可用点数: 10)</label>
        <input type="number" id="intelligence" name="intelligence" min="0" max="10" required>
        <label for="constitution">体质</label>
        <input type="number" id="constitution" name="constitution" min="0" max="10" required>
        <label for="charm">魅力</label>
        <input type="number" id="charm" name="charm" min="0" max="10" required>
        <label for="wealth">财富</label>
        <input type="number" id="wealth" name="wealth" min="0" max="10" required>
        <input type="submit" name="start" value="开始人生">
    </form>
</body>

</html>